<?php namespace Phpcmf\Controllers\Admin;

class Field extends \Phpcmf\Common
{
    public $config;
    public $module;

    public function __construct(...$params) {
        parent::__construct(...$params);
        \Phpcmf\Service::V()->assign([
            'menu' => \Phpcmf\Service::M('auth')->_admin_menu(
                [
                    '字段划分' => ['order/field/index', 'fa fa-cog'],
                    '自定义字段' => ['url:'.\Phpcmf\Service::L('Router')->url('field/index', ['rname' => 'order', 'rid' => SITE_ID]), 'fa fa-code'],
                    'help' => [640],
                ]
            ),
            'uriprefix' => 'order/field'
        ]);
        $this->config = \Phpcmf\Service::M('app')->get_config(APP_DIR);

        $module = \Phpcmf\Service::L('cache')->get('module-'.SITE_ID.'-content');
        if (!$module) {
            $this->_admin_msg(0, dr_lang('未安装任何内容模块'));
        }

        $data = [];
        foreach ($module as $t) {
            if ($this->config['module'][$t['dirname']]['open']) {
                $data[$t['dirname']] = \Phpcmf\Service::L('cache')->get('module-'.SITE_ID.'-'.$t['dirname']);
            }
        }
        if (!$data) {
            $this->_admin_msg(0, dr_lang('未安装任何内容模块'));
        }
        $this->module = $data;
        $this->field = \Phpcmf\Service::M()->db->table('field')->where('disabled', 0)->where('relatedname', 'order')->where('relatedid', SITE_ID)->orderBy('displayorder ASC,id ASC')->get()->getResultArray();
        if ($this->field) {
            foreach ($this->field as $t) {
                if (!isset($this->config['field'][SITE_ID][$t['id']])) {
                    $this->config['field'][SITE_ID][$t['id']] = [];
                }
            }
        }
    }

    public function index() {

        \Phpcmf\Service::V()->assign([
            'field' => $this->field,
            'module' => $this->module,
            'data' => $this->config['field'][SITE_ID],
        ]);
        \Phpcmf\Service::V()->display('field.html');
    }

    public function add() {

        $ids = \Phpcmf\Service::L('input')->get_post_ids();
        !$ids && $this->_json(0, dr_lang('你还没有选择呢'));

        $mid = dr_safe_filename(\Phpcmf\Service::L('input')->post('mid'));
        !$mid && $this->_json(0, dr_lang('你还没有选择模块'));

        if ($this->field) {
            foreach ($this->field as $t) {
                if (in_array($t['id'], $ids)) {
                    $this->config['field'][SITE_ID][$t['id']][] = $mid;
                }
            }
        }

        \Phpcmf\Service::M('app')->save_config(APP_DIR, $this->config);

        $this->_json(1, dr_lang('划分成功'));
    }


    public function del() {

        $mid = dr_safe_filename(\Phpcmf\Service::L('input')->get('mid'));
        !$mid && $this->_json(0, dr_lang('你还没有选择模块!'));

        $fid = (int)\Phpcmf\Service::L('input')->get('fid');
        !$fid && $this->_json(0, dr_lang('字段id不存在'));


        if (in_array($mid, $this->config['field'][SITE_ID][$fid])) {
            $new = [];
            foreach ($this->config['field'][SITE_ID][$fid] as $t) {
                if ($t != $mid) {
                    $new[] = $t;
                }
            }
            $this->config['field'][SITE_ID][$fid] = $new;
            \Phpcmf\Service::M('app')->save_config(APP_DIR, $this->config);
        }

        $this->_json(1, dr_lang('删除成功'));
    }


}
